import HashMap from '@ohos.util.HashMap'
import {
  LogManager
} from '../01-LogManager/Logger_B/LogManager';
import {
  TaskSta
} from './TaskSta';

/**
 * TaskSta
 * 描述一个任务,进行统计和分析.
 *
 */
export enum TaskState {
  //任务开始.
  Start = "start",
  //任务中途操作.
  Step = "step",
  //任务结束,成功.
  Success = "success",
  //任务结束,失败.
  Error = "error",
  //任务结束,取消.
  Cancel = "cancel"
}


export class StatisticsManager {
  static map = new HashMap<string, TaskSta>();

  /**
   * 开始一个任务,如果已经有这个任务,报异常.
   * @param key
   */
  static startTaskSta(key: string): void {
    let sta: TaskSta = StatisticsManager.getTaskSta(key);
    if (sta) {
      LogManager.error(`Task`, "duplicate start task!");

    }
    sta = new TaskSta(key);
    StatisticsManager.map.set(key, sta);
  }

  static getTaskSta(key: string): TaskSta {
    return StatisticsManager.map.get(key);
  }

  static finishTask(key: string): string {
    let sta = StatisticsManager.map.get(key);
    if (!sta) {
      LogManager.debug(`${key}`, "no task found");
      return "";
    } else {
      StatisticsManager.map.remove(key);
      return sta.end(true);
    }
  }

  /**
   * @deprecated
   * using startTaskSta instead.
   * @param key
   */
  static startTimeCostSta(key: string) {
    let sta = new TaskSta(key);
    StatisticsManager.map.set(key, sta);
  }

  /**
   * @deprecated
   * using endTaskSta instead.
   * @param key
   */
  static endTimeCostSta(key: string) {
    let sta = StatisticsManager.map.get(key);
    if (!sta) {
      LogManager.debug(`${key}`, "no task found");
    } else {
      sta.end(true);
      LogManager.debug(`${key}`, `const ${sta.getCost()} ms.`)
    }
  }
}